
var EpgMenu = Class.create(EpgWidget,{
	initialize: function($super, id, opts,links) {
        $super(id,opts,links);
        this.displayCount = opts.displayCount;
        if(opts.direction) {
        	this.direction  = opts.direction;
        } else {
        	this.direction  = "y";
        }
        this.beginIndex = 0;
		this.selectedIndex = 0;
		this.cursorIndex = 0;
		this.delayMSTime = 0;
		if (opts.delayMSTime){
			this.delayMSTime = opts.delayMSTime;
		}
		this.timer = null;
        if(opts.current){
        	this.current =opts.current;
        	this.selectedIndex = this.index(this.current);
        	this.cursorIndex = this.selectedIndex;
			if(this.selectedIndex>=this.displayCount){
				var old = this.current
				this.current = this.links[0];
				this.scroll(this.selectedIndex); 
				this.current = old;
				this.beginIndex = this.selectedIndex - this.displayCount+1;
			}
        	$("#"+this.current+"_text").removeClass("menu_item");
        	$("#"+this.current+"_text").addClass(opts.currentClass);
			
        }else{
        	this.current = links[0];
        }   
        this.refreshflag();
       /* this.focusCursor = epgPage.findLink(this.current).focusCursor;
        for(var i=0;i<this.links.length;i++){
       	 var aLink =   epgPage.findLink(this.current);
       	 aLink.focusCursor = null;
       }*/
    },
    lastIndex:function(){
		var lastIndex = this.beginIndex+this.displayCount-1 ;
		return lastIndex;
	},
	scrollUp:function(idx){
		if(idx <0) return false;
		if(this.beginIndex>0){
		    $("#" + this.id).scrollTo("-="+Math.abs(this.opts.step) +"px",{axis: this.direction });
			this.beginIndex --;
		}
		this.refreshflag();
		if(this.delayMSTime!=0){
			this.delayOpenCurrentLink();
		}
	},
	scrollDown:function(idx){
		if(idx >= this.links.length) return false;

		if((idx>this.displayCount-1)){
			$("#" + this.id).scrollTo("+="+Math.abs(this.opts.step) +"px",{axis: this.direction });
			this.beginIndex ++;
		}
		this.refreshflag();
		if(this.delayMSTime!=0){
			this.delayOpenCurrentLink();
		}
	},
	delayOpenCurrentLink:function(){ 
		var that = this;
		clearTimeout(this.timer);
		this.timer = setTimeout(that.openCurrentLink,this.delayMSTime);
	},
	openCurrentLink:function(){
		epgPage.gotoPage();
		this.timer = null;
	},
	scroll:function(idx) {
		var result = false;
		var lastIndex = this.lastIndex();
		if(idx >= this.links.length) return false;
		if((idx<=lastIndex) && (idx>=this.beginIndex)){
			result =  false;
		}else if(idx<this.beginIndex){
			var	scrollStep = idx-this.beginIndex;
			$("#" + this.id).scrollTo("-="+Math.abs(this.opts.step*scrollStep) +"px",{axis: this.direction });
			this.beginIndex= this.beginIndex+scrollStep;
			this.cursorIndex = 0;
			result = true;
		}else{
				var scrollStep = idx-lastIndex;
				$("#" + this.id).scrollTo("+="+Math.abs(this.opts.step*scrollStep) +"px",{axis: this.direction });
				this.beginIndex= this.beginIndex+scrollStep;
				this.cursorIndex = this.displayCount-1;
				result= true;
		}
		this.refreshflag();
		return result;
	},
    refreshflag : function() {
    	if(this.links.length<=this.displayCount){
    		$("#" + this.id + "_up_flag").css("display", "none");
    		$("#" + this.id + "_down_flag").css("display", "none");
    		return ;
    	}
		if (this.beginIndex == 0) {
			$("#" + this.id + "_up_flag").css("display", "none");
		} else {
			$("#" + this.id + "_up_flag").css("display", "")
		}
		if(this.count() == this.lastIndex()+1){
			$("#" + this.id + "_down_flag").css("display", "none");
		} else {
			$("#" + this.id + "_down_flag").css("display", "")
		}
	},
 	focus:function(id){
		var cursorIndex =  this.cursorIndex;
		this.scroll(cursorIndex);
		var cursor = epgPage.findLink(this.links[cursorIndex]);
		var idx = this.beginIndex + cursorIndex;
	    target = epgPage.findLink(this.links[idx]);
		if(target){
			epgPage.cursorEffect(cursor);
			epgPage.effect(target);
			epgPage.currentLink = target;
			return true;
		}
		return false;
	
	},
	blur:function(id){
		//this.scroll(this.cursorIndex);
		//this.cursorIndex = this.index(id);
	},
	onkeypress : function(ev) {
		var rowId = ev.target.id;
		keycode = ev.which;
		if (this.hasElement(rowId)) {
			var rowIndex = this.index(rowId);
			if(rowIndex ==-1){
				rowIndex = this.index(this.current);
			}
			if(this.direction=='y') {
				switch (keycode) {
				case KEY_ENUM.KEY_UP:
					result  = this.scrollUp(rowIndex-1);
					return result;
					//this.focus(this.links[rowIndex-1])	
				case KEY_ENUM.KEY_DOWN:
					result =  this.scrollDown(rowIndex+1);
					return result;
					//this.focus(this.links[rowIndex+1])	
				}
			} else {
				switch (keycode) {
				case KEY_ENUM.KEY_LEFT:
					result  = this.scrollUp(rowIndex-1);
					return result;
					//this.focus(this.links[rowIndex-1])	
				case KEY_ENUM.KEY_RIGHT:
					result =  this.scrollDown(rowIndex+1);
					return result;
					//this.focus(this.links[rowIndex+1])	
				}
			}
		}
		return false;
	}
});
